package com.lemon.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lemon.domain.PageResult;
import com.lemon.domain.Warehouse;
import com.lemon.domain.WarehouseArea;
import com.lemon.domain.vo.WarehousAreaVo;
import com.lemon.mapper.WarehouseAreaMapper;
import com.lemon.mapper.WarehouseMapper;
import com.lemon.service.WarehouseAreaService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class WarehouseAreaServiceImpl extends ServiceImpl<WarehouseAreaMapper, WarehouseArea> implements WarehouseAreaService {

    private final WarehouseAreaMapper warehouseAreaMapper;

    private final WarehouseMapper warehouseMapper;

    @Override
    public PageResult getPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<WarehouseArea> warehouseAreas = warehouseAreaMapper.selectList(null);
        Page pageInfo = (Page) warehouseAreas;
        return new PageResult(pageInfo.getTotal(),pageInfo.getResult());
    }

    @Override
    public void add(WarehouseArea warehouseArea) {
        Warehouse warehouse = warehouseMapper.selectById(warehouseArea.getWarehouseId());
        warehouseArea.setWarehouseName(warehouse.getName());
        warehouseArea.setCreateTime(LocalDateTime.now());
        warehouseAreaMapper.insert(warehouseArea);
    }

    @Override
    public void updateWarehouseArea(WarehouseArea warehouseArea) {
        Warehouse warehouse = warehouseMapper.selectById(warehouseArea.getWarehouseId());
        warehouseArea.setWarehouseName(warehouse.getName());
        warehouseArea.setUpdateTime(LocalDateTime.now());
        warehouseAreaMapper.updateById(warehouseArea);
    }

    @Override
    public List<WarehousAreaVo> getWarehouseAreaList(Integer warehouseId) {
        List<WarehousAreaVo> vo = warehouseAreaMapper.getWarehouseAreaList(warehouseId);
        return vo;
    }
}
